package com.hjp.designmode.builder.domain;

import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * @Description: builder模式, card2为用@Builder实现builder模式源码
 * @Author: huangjp6
 * @Date: 2022/3/24 10:53
 */
public class Card2 {

    private int id = 1;
    private String name = "huangjp6";
    private boolean sex = false;

    // 这种有默认值情况默认值会被清除掉,默认值(注释)需要在Card1.CardBuilder构造方法里面
    public Card2() {

    }

    public Card2(int id, String name, boolean sex) {
        this.id = id;
        this.name = name;
        this.sex = sex;
    }

    public static Card2.CardBuilder builder() {
        return new Card2.CardBuilder();
    }

    public static class CardBuilder {
        private int id;
        private String name;
        private boolean sex;

        CardBuilder() {
//            this.id = 1;
//            this.name = "huangjp6";
        }


        public Card2.CardBuilder id(int id) {
            this.id = id;
            return this;
        }

        public Card2.CardBuilder name(String name) {
            this.name = name;
            return this;
        }

        public Card2.CardBuilder sex(boolean sex) {
            this.sex = sex;
            return this;
        }

        public Card2 build() {
            return new Card2(this.id, this.name, this.sex);
        }

        public String toString() {
            return "Card.CardBuilder(id=" + this.id + ", name=" + this.name + ", sex=" + this.sex + ")";
        }
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("id", id)
                .append("name", name)
                .append("sex", sex)
                .toString();
    }

    public static void main(String[] args) {
        Card2 build = new CardBuilder().build();
        System.out.println(build);
    }
}
